package com.wandian.news.mvvm.knowledge.wordtool;

import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.adapter.KnowledgeCYDGRecycleAdapter;
import com.wandian.news.adapter.KnowledgeMYJJRecycleAdapter;
import com.wandian.news.adapter.KnowledgeXHZDRecycleAdapter;
import com.wandian.news.databinding.NewsActivityKnowledgeWordToolBinding;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 文字转换activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NEWS_WORD_TOOL)
public class KnowledgeWordToolActivity extends BaseMVVMActivity<KnowledgeWordToolViewModel>   {
    private NewsActivityKnowledgeWordToolBinding dataBinding;
    private KnowledgeXHZDRecycleAdapter adapterXHZD;
    private KnowledgeCYDGRecycleAdapter adapterCYDG;
    private KnowledgeMYJJRecycleAdapter adapterMYJJ;
    private String  title = "";
    private String  jfzhType = "0";
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.news_activity_knowledge_word_tool);
        dataBinding.setLifecycleOwner(this);
        title = getIntent().getStringExtra("title")+"";
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
        initRecycle();

    }

    private void initRecycle(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        dataBinding.recycler.setLayoutManager(layoutManager);
    }

    private void getData(String word){
        switch (title){
            case "近反义词":
                dataBinding.llJfyc.setVisibility(View.VISIBLE);
                mViewModel.getJFYCData(word);
                mViewModel.dataJFYCBean.observe(this,dataJFYCBean->{
                    if(!UIUtils.isEmpty(dataJFYCBean)){
                        dataBinding.tvTitleJfyc.setText("搜索词："+dataJFYCBean.getWords()+"");
                        dataBinding.tvTitleFyc.setText("反义词："+dataJFYCBean.getFyc()+"");
                        dataBinding.tvTitleJyc.setText("近义词："+dataJFYCBean.getJyc()+"");
                    }
                });
                break;
            case "新华字典":
                adapterXHZD = new KnowledgeXHZDRecycleAdapter(R.layout.news_item_knowledge_xhzd_style);
                dataBinding.recycler.setAdapter(adapterXHZD);
                dataBinding.recycler.setVisibility(View.VISIBLE);
                mViewModel.getXHZDData(word);
                mViewModel.dataXHZDBean.observe(this,dataXHZDBean->{
                    if(!UIUtils.isEmpty(dataXHZDBean) && dataXHZDBean.size()>0){
                        adapterXHZD.setNewData(dataXHZDBean);
                    }
                });
                break;
            case "成语典故":
                adapterCYDG = new KnowledgeCYDGRecycleAdapter(R.layout.news_item_knowledge_cydg_style);
                dataBinding.recycler.setAdapter(adapterCYDG);
                dataBinding.recycler.setVisibility(View.VISIBLE);
                mViewModel.getCYDGData(word);
                mViewModel.dataCYDGBean.observe(this,dataCYDGBean->{
                    if(!UIUtils.isEmpty(dataCYDGBean) && dataCYDGBean.size()>0){
                        adapterCYDG.setNewData(dataCYDGBean);
                    }
                });
                break;
            case "名言警句":
                adapterMYJJ = new KnowledgeMYJJRecycleAdapter(R.layout.news_item_knowledge_myjj_style);
                dataBinding.recycler.setAdapter(adapterMYJJ);
                dataBinding.recycler.setVisibility(View.VISIBLE);
                mViewModel.getMYJJData();
                mViewModel.dataMYJJBean.observe(this,dataMYJJBean->{
                    if(!UIUtils.isEmpty(dataMYJJBean) && dataMYJJBean.size()>0){
                        adapterMYJJ.setNewData(dataMYJJBean);
                    }
                });
                break;
            case "中文词典":
                dataBinding.llYhzd.setVisibility(View.VISIBLE);
                mViewModel.getZWCDData(word);
                mViewModel.dataYHZDBean.observe(this,dataYHZDBean->{
                    if(!UIUtils.isEmpty(dataYHZDBean)  ){
                        dataBinding.tvWord.setText("搜索词："+dataYHZDBean.getWord()+"");
                        dataBinding.tvWordContent.setText("解释："+dataYHZDBean.getContent()+"");
                    }
                });
                break;
            case "英汉词典":
                dataBinding.llYhzd.setVisibility(View.VISIBLE);
                mViewModel.getYHZDData(word);
                mViewModel.dataYHZDBean.observe(this,dataYHZDBean->{
                    if(!UIUtils.isEmpty(dataYHZDBean)  ){
                        dataBinding.tvWord.setText("搜索词："+dataYHZDBean.getWord()+"");
                        dataBinding.tvWordContent.setText("翻译："+dataYHZDBean.getContent()+"");
                    }
                });
                break;
            case "简繁转换":
                dataBinding.llJfzh.setVisibility(View.VISIBLE);
                mViewModel.getJFZHData(word,jfzhType);
                mViewModel.dataJFZHBean.observe(this,dataJFZHBean->{
                    if(!UIUtils.isEmpty(dataJFZHBean)  ){
                        dataBinding.tvIntext.setText("转换前："+dataJFZHBean.getIntext()+"");
                        dataBinding.tvOuttext.setText("转换后："+dataJFZHBean.getOuttext()+"");
                    }
                });
                break;
        }

    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText(title+"");
        switch (title){
            case "近反义词":
               dataBinding.etSearch.setHint("请输入查询字、词、成语");
                break;
            case "新华字典":
                dataBinding.etSearch.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
                dataBinding.etSearch.setHint("请输入查询的汉字");
                break;
            case "中文词典":
                dataBinding.etSearch.setHint("请输入查询的词语");
                break;
            case "名言警句":
                dataBinding.rlSearch.setVisibility(View.GONE);
                getData("");
                break;
            case "成语典故":
                dataBinding.etSearch.setHint("请输入查询的成语");
                break;
            case "英汉词典":
                dataBinding.etSearch.setHint("请输入查询的英文单词");
                break;
            case "简繁转换":
                dataBinding.llJfzhType.setVisibility(View.VISIBLE);
                dataBinding.etSearch.setHint("请输入转换的文字");
                break;
        }
    }


    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        dataBinding.tvSearch.setOnClickListener(v -> {
            finishhideSoftBoard();
            if(!UIUtils.isEmpty(dataBinding.etSearch.getText())){
                getData(dataBinding.etSearch.getText().toString());
            }else {
                ToastUtil.show("请输入搜索类容");
            }
        });
        dataBinding.ivFt.setOnClickListener(v -> {
            jfzhType = "0";
            dataBinding.ivFt.setImageResource(R.drawable.img_item_choosed);
            dataBinding.ivJt.setImageResource(R.drawable.img_item_unchoose);
        });
        dataBinding.ivJt.setOnClickListener(v -> {
            jfzhType = "1";
            dataBinding.ivFt.setImageResource(R.drawable.img_item_unchoose);
            dataBinding.ivJt.setImageResource(R.drawable.img_item_choosed);
        });
    }



    @Override
    protected void onDestroy() {
        finishhideSoftBoard();
        super.onDestroy();
    }


}
